<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<!--				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"-->
				<!--						   @click="batch_del"></el-button>-->
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="部门名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :data="tableData" row-key="id" @selection-change="selectionChange"
					 @getTableData="getTableData">
				<!--				<el-table-column type="selection" width="50"></el-table-column>-->
				<el-table-column label="ID" prop="id" width="150"></el-table-column>
				<el-table-column label="部门名称" prop="name" width="250"></el-table-column>
				<el-table-column label="更新时间" prop="createDatetime" width="180"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="270">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">
								查看
							</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">
								编辑
							</el-button>
							<el-button text type="primary" size="small" @click="jkgl(scope.row, scope.$index)">
								接口关联
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<el-dialog
		v-model="dialogVisible"
		title="接口关联"
		width="60%"
		:before-close="handleClose"

		filterable>
		<el-transfer
			:titles="['未关联', '已关联']"
			filterable
			@change="dialogChange" v-model="relApiList" :data="apiList"/>
		<template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitRelApi">确定</el-button>
      </span>
		</template>
	</el-dialog>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
				 @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	import {deleteDept, deptRelApi, getDeptById, getDeptList} from "@/api/model/dept";
	import {getApiList} from "@/api/model/jk";

	export default {
		name: 'dept',
		components: {
			saveDialog
		},
		data() {
			return {
				dialogVisible: false,
				dialog: {
					save: false
				},
				apiObj: this.$API.system.dept.list,
				tableData: [],
				selection: [],
				pageNum: 1,
				pageSize: 10,
				total: 0,
				search: {
					keyword: null
				},
				apiList: [],
				relApiList: [],
				relDeptId: '',
			}
		},
		created() {
			this.init()
		},
		methods: {
			submitRelApi() {
				deptRelApi({
					"apiList": this.relApiList,
					"deptId": this.relDeptId
				}).then(res => {
					if (res.code === 200) {
						this.$message.success("关联成功！")
						this.dialogVisible = false
					} else {
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				})

			},
			dialogChange(value, direction, movedKeys) {
				console.log('aaaa');
				console.log(value.value);
				console.log(direction);
				console.log(movedKeys);
				console.log(this.relApiList);
				console.log('bbb');
			},
			jkgl(row) {
				this.relDeptId = row.id;
				this.dialogVisible = true
				getApiList({
					"pageNum": 1,
					"pageSize": 999,
				}).then((res) => {
					this.apiList = [];
					this.relApiList = [];


					getApiList({
						"pageNum": 1,
						"pageSize": 999,
						"deptId": row.id
					}).then(relRes => {
						let relList = relRes.data.list;
						relList.forEach(j => {
							this.relApiList.push(j.id)
						})
						let list = res.data.list;
						list.forEach(e => {
							this.apiList.push({
								key: e.id,
								label: e.name,
							})
						})
					})

				})
			},
			getTableData(data) {
				this.pageNum = data.pageNum
				this.pageSize = data.pageSize
				this.init()
			},
			init() {
				getDeptList({pageNum: this.pageNum, pageSize: this.pageSize, name: this.search.keyword}).then(res => {
					console.log(res)
					this.tableData = res.data;
				})
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					getDeptById({'deptId': row.id}).then(res => {
						this.$refs.saveDialog.open('edit').setData(res.data)
					})
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					getDeptById({'deptId': row.id}).then(res => {
						this.$refs.saveDialog.open('show').setData(res.data)
					})
				})
			},
			//删除
			async table_del(row) {
				deleteDept({'deptId': row.id}).then(res => {
					if (res.code === 200) {
						this.$message.success("删除成功")
						this.init()
					} else {
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				})
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.$refs.table.refresh()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//搜索
			upsearch() {
				this.init()
			},
			//根据ID获取树结构
			filterTree(id) {
				var target = null;

				function filter(tree) {
					tree.forEach(item => {
						if (item.id == id) {
							target = item
						}
						if (item.children) {
							filter(item.children)
						}
					})
				}

				filter(this.$refs.table.tableData)
				return target
			},
			//本地更新数据
			handleSaveSuccess() {
				this.init();
				// if (mode == 'add') {
				// 	this.$refs.table.refresh()
				// } else if (mode == 'edit') {
				// 	this.$refs.table.refresh()
				// }
			}
		}
	}
</script>

<style>
	.el-transfer-panel {
		width: 350px;
	}
</style>
